package org.jeecg.modules.demo.hd.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.hd.entity.AppGoldProject;
import org.jeecg.modules.demo.hd.entity.AppSpace;
import org.jeecg.modules.demo.hd.service.IAppGoldProjectService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.demo.hd.service.IAppSpaceService;
import org.jeecg.modules.demo.hd.vo.GoldProjectModelVo;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 金币项目
 * @Author: jeecg-boot
 * @Date: 2025-04-30
 * @Version: V1.0
 */
@Api(tags = "金币项目")
@RestController
@RequestMapping("/hd/appGoldProject")
@Slf4j
public class AppGoldProjectController extends JeecgController<AppGoldProject, IAppGoldProjectService> {
    @Autowired
    private IAppGoldProjectService appGoldProjectService;

    @Autowired
    private IAppSpaceService spaceService;

    /**
     * 分页列表查询
     *
     * @param appGoldProject
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "app_gold_project-分页列表查询")
    @ApiOperation(value = "app_gold_project-分页列表查询", notes = "app_gold_project-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<AppGoldProject>> queryPageList(AppGoldProject appGoldProject,
                                                       @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                       HttpServletRequest req) {
        QueryWrapper<AppGoldProject> queryWrapper = QueryGenerator.initQueryWrapper(appGoldProject, req.getParameterMap());
        Page<AppGoldProject> page = new Page<AppGoldProject>(pageNo, pageSize);
        IPage<AppGoldProject> pageList = appGoldProjectService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @ApiOperation(value = "获取黄金项目", notes = "获取黄金项目")
    @GetMapping(value = "/getAllGoldProject")
    public Result<List<GoldProjectModelVo>> getAllGoldProject(HttpServletRequest request) {
        LambdaQueryWrapper<AppGoldProject> goldProjectLambdaQueryWrapper = Wrappers.lambdaQuery();
        goldProjectLambdaQueryWrapper.eq(AppGoldProject::getStatus, 1);
        List<AppGoldProject> list = appGoldProjectService.list(goldProjectLambdaQueryWrapper);
        // 获取所有奖励广告和广告位状态为0的广告
        List<AppSpace> spaceList = spaceService.list().stream().
                filter(x -> x.getSpaceType().equals(0) && x.getSpaceState().equals(0)).collect(Collectors.toList());
        List<GoldProjectModelVo> collect = list.stream().map(item -> {
            GoldProjectModelVo vo = new GoldProjectModelVo();
            BeanUtils.copyProperties(item, vo);
            if (spaceList != null && spaceList.size() > 0) {
                Collections.shuffle(spaceList); // 这会改变list的顺序，但不会影响后续的操作。
                vo.setSpaceId(spaceList.get(0).getSpaceId());
            } else {
                vo.setSpaceId("103437211");
            }
            return vo;
        }).collect(Collectors.toList());
        return Result.OK(collect);
    }

    /**
     * 添加
     *
     * @param appGoldProject
     * @return
     */
    @AutoLog(value = "app_gold_project-添加")
    @ApiOperation(value = "app_gold_project-添加", notes = "app_gold_project-添加")
    @RequiresPermissions("hd:app_gold_project:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody AppGoldProject appGoldProject) {
        appGoldProjectService.save(appGoldProject);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param appGoldProject
     * @return
     */
    @AutoLog(value = "app_gold_project-编辑")
    @ApiOperation(value = "app_gold_project-编辑", notes = "app_gold_project-编辑")
    @RequiresPermissions("hd:app_gold_project:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody AppGoldProject appGoldProject) {
        appGoldProjectService.updateById(appGoldProject);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "app_gold_project-通过id删除")
    @ApiOperation(value = "app_gold_project-通过id删除", notes = "app_gold_project-通过id删除")
    @RequiresPermissions("hd:app_gold_project:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        appGoldProjectService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "app_gold_project-批量删除")
    @ApiOperation(value = "app_gold_project-批量删除", notes = "app_gold_project-批量删除")
    @RequiresPermissions("hd:app_gold_project:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.appGoldProjectService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "app_gold_project-通过id查询")
    @ApiOperation(value = "app_gold_project-通过id查询", notes = "app_gold_project-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<AppGoldProject> queryById(@RequestParam(name = "id", required = true) String id) {
        AppGoldProject appGoldProject = appGoldProjectService.getById(id);
        if (appGoldProject == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(appGoldProject);
    }

}
